<template>
	<view class="coupon-list">
		<!-- 搜索框 -->
		<uni-easyinput class="query-input" suffixIcon="search" v-model="queryStockName" :trim="true"
			confirmType="search" placeholder="卡券名称查询" @confirm="handleCouponQuery" @iconClick="handleCouponQuery"></uni-easyinput>


		<!-- 卡券列表 -->
		<view class="coupon-tab">
			<view class="tab-item" :class="{'active': couponTabIndex == 0}" @click="toggleCouponTab(0)">生效中</view>
			<view class="tab-item" :class="{'active': couponTabIndex == 1}" @click="toggleCouponTab(1)">已过期</view>
		</view>
		<view class="coupon-box">

			<!-- 正常卡券列表 -->
			<view v-show="couponTabIndex == 0">
				<view v-if="couponList.length > 0">
					<view class="coupon-item" v-for="(item, index) in couponList" :key="index"
						@click="handleEditCoupon(item.queryBusifavorStock.stock_id)">
						<view class="header-box">
							<view class="header-left-box">
								<view class="coupon-type" v-if="item.queryBusifavorStock.stock_type == 'NORMAL'">满减券
								</view>
								<view class="coupon-type" v-else-if="item.queryBusifavorStock.stock_type == 'DISCOUNT'">
									折扣券
								</view>
								<view class="coupon-type" v-else-if="item.queryBusifavorStock.stock_type == 'EXCHANGE'">
									换购券
								</view>
								<view class="get-coupon-num">
									已领取{{item.queryBusifavorStock.send_count_information.total_send_num}}张</view>
							</view>
							<view class="header-right-box">
								<view class="coupon-name">{{item.queryBusifavorStock.stock_name}}</view>
								<view class="coupon-date">
									可核销时间：{{item.queryBusifavorStock.coupon_use_rule.coupon_available_time.available_begin_time}}至{{item.queryBusifavorStock.coupon_use_rule.coupon_available_time.available_end_time}}
								</view>
							</view>
						</view>

						<view class="line"></view>

						<view class="bottom-box">适用商品范围：{{item.queryBusifavorStock.goods_name}}</view>
						<!-- 剩余卡券数量 -->
						<view class="residue-box">
							<text
								class="residue-num">x{{item.queryBusifavorStock.stock_send_rule.max_coupons - item.queryBusifavorStock.send_count_information.total_send_num}}</text>
						</view>
					</view>
				</view>


				<!-- 无数据提示 -->
				<view class="no-data-tips" v-else>
					<uni-icons type="info" size="30" color="#999"></uni-icons>
					<text>暂无数据</text>
				</view>
			</view>


			<!-- 已过期列表 -->
			<view v-show="couponTabIndex == 1">
				<view v-if="expiredCouponList.length > 0">
					<view class="coupon-item" v-for="(item, index) in expiredCouponList" :key="index"
						@click="handleExpiredClick">
						<view class="header-box">
							<view class="header-left-box expired">
								<view class="coupon-type" v-if="item.queryBusifavorStock.stock_type == 'NORMAL'">满减券
								</view>
								<view class="coupon-type" v-else-if="item.queryBusifavorStock.stock_type == 'DISCOUNT'">
									折扣券
								</view>
								<view class="coupon-type" v-else-if="item.queryBusifavorStock.stock_type == 'EXCHANGE'">
									换购券
								</view>
								<view class="get-coupon-num">
									已领取{{item.queryBusifavorStock.send_count_information.total_send_num}}张</view>
							</view>
							<view class="header-right-box">
								<view class="coupon-name">{{item.queryBusifavorStock.stock_name}}</view>
								<view class="coupon-date">
									可核销时间：{{item.queryBusifavorStock.coupon_use_rule.coupon_available_time.available_begin_time}}至{{item.queryBusifavorStock.coupon_use_rule.coupon_available_time.available_end_time}}
								</view>
							</view>
						</view>

						<view class="line"></view>

						<view class="bottom-box">适用商品范围：{{item.queryBusifavorStock.goods_name}}</view>
						<!-- 卡券过期提示 -->
						<view class="residue-box expired">
							<text class="residue-num expired-num">已过期</text>
						</view>
					</view>
				</view>


				<!-- 无数据提示 -->
				<view class="no-data-tips" v-else>
					<uni-icons type="info" size="30" color="#999"></uni-icons>
					<text>暂无数据</text>
				</view>
			</view>

		</view>

		<view class="height"></view>
	</view>
</template>

<script>
	import apiRqs from '@/api/api-request.js';
	import getDateTime from '@/common/getDateTime.js';
	export default {
		data() {
			return {
				screenIndex: 0,
				couponTabIndex: 0,
				screenList: [{
						id: 1,
						name: '全部'
					},
					{
						id: 2,
						name: '抵扣券'
					},
					{
						id: 3,
						name: '换购券'
					},
					{
						id: 4,
						name: '满减券'
					},
					{
						id: 5,
						name: '审核中'
					},
				],
				couponList: [],
				expiredCouponList: [],
				paged: {
					"pageSize": 9999,
					"pageIndex": 1,
					"orderBy": "asc"
				},
				queryStockName: "",
				availableCouponList: [],
			}
		},
		onLoad() {
			this.getCouponList();
		},
		methods: {
			toggleCouponTab(index) {
				this.couponTabIndex = index;
			},
			// 切换筛选器
			handleScreenToggle(idx) {
				this.screenIndex = idx;
			},
			// 点击卡券进入修改
			handleEditCoupon(id) {
				uni.navigateTo({
					url: "/packageD/coupon/edit-coupon?id=" + id
				})
			},
			// 获取卡券列表
			getCouponList() {
				getApp().showLoading();
				apiRqs._getCouponList(this.paged).then((res) => {
					console.log('卡券列表', res);
					uni.hideLoading();
					if (res.code == 200) {
						let result = JSON.parse(JSON.stringify(res.data.data));
						let normalResult = [];
						let expiredResult = [];
						const availableCouponList = result.filter(obj => obj.queryBusifavorStock.resultCode
							.stateCode === "200");

						const formatAvailableCouponList = availableCouponList.map(item => {
							item.queryBusifavorStock.coupon_use_rule.coupon_available_time
								.available_begin_time = item.queryBusifavorStock
								.coupon_use_rule
								.coupon_available_time.available_begin_time.split("T")[0];

							item.queryBusifavorStock.coupon_use_rule.coupon_available_time
								.available_end_time = item.queryBusifavorStock
								.coupon_use_rule
								.coupon_available_time.available_end_time.split("T")[0];

							let dayNum = getDateTime.getDaysBetween(item
								.queryBusifavorStock
								.coupon_use_rule
								.coupon_available_time
								.available_end_time, getDateTime.getDay(0));
							if (dayNum > 0) {
								item.isExpired = true;
								normalResult.push(item);
							} else {
								item.isExpired = false;
								expiredResult.push(item);
							}
							return item;
						})
						this.availableCouponList = formatAvailableCouponList;
						console.log("可用卡券列表", formatAvailableCouponList);
						this.couponList = normalResult;
						this.expiredCouponList = expiredResult;
					} else {
						getApp().showToast(res.msg);
					}
				})
			},
			handleExpiredClick() {
				getApp().showToast('该券已过期，无法修改');
			},
			// stock_name查询卡券
			handleCouponQuery() {
				const availableCouponList = this.availableCouponList;
				let newArray = availableCouponList.filter(obj => new RegExp(this.queryStockName, 'i').test(obj.stockName));
				console.log(newArray);
				const normalResult = [];
				const expiredResult = [];
				newArray.map(item => {
					if (item.isExpired) {
						normalResult.push(item);
					} else {
						expiredResult.push(item);
					}
				})
				this.couponList = normalResult;
				this.expiredCouponList = expiredResult;
			},
		},
	}
</script>

<style lang="less">
	page {
		background-color: #f5f5f5;
	}

	.coupon-list {
		.height {
			height: 30px;
		}

		.search-box {
			margin: 20px;
			display: flex;
			align-items: center;
			border-radius: 4px;
			background-color: #fff;
			box-shadow: 0px 5px 11px 0px rgba(0, 0, 0, 0.05);
			padding: 10px 15px;
			box-sizing: border-box;

			.search-icon {
				font-size: 18px;
				color: rgba(0, 0, 0, 0.3);
				width: 18px;
				flex: 0 0 18px;
			}

			.search-input {
				flex: 1;
				min-width: 0;
				margin: 0px 10px;
				border: none;
				font-size: 12px;
				color: #666;
			}

			.placeholderClass {
				color: #ddd;
			}
		}

		.uni-easyinput {
			width: calc(100% - 40px);
			margin: 20px;
			box-shadow: 0px 5px 11px 0px rgba(0, 0, 0, 0.05);
		}

		.screen-box {
			margin: 15px 20px 0 20px;
			display: flex;
			align-items: center;

			.screen-item {
				flex: 1;
				height: 31px;
				line-height: 31px;
				text-align: center;
				font-size: 13px;
				color: rgba(0, 0, 0, 0.8);
				background-color: rgba(0, 0, 0, 0.03);
				border-radius: 4px;
				margin-left: 10px;

				&:first-child {
					margin-left: 0;
				}

				&.active {
					background-color: rgba(255, 165, 61, 0.10);
					color: #FFA53D;
					font-weight: 500;
				}
			}
		}

		.coupon-tab {
			margin: 20px;
			display: flex;
			align-items: center;

			.tab-item {
				flex: 1;
				text-align: center;
				color: rgba(0, 0, 0, 0.5);
				font-size: 13px;
				position: relative;

				&.active {
					color: #FFA53D;
					font-weight: 500;

					&::after {
						display: inline-block;
						content: "";
						width: 12px;
						height: 3px;
						position: absolute;
						left: 50%;
						bottom: -6px;
						margin-left: -5px;
						background-color: #FFA53D;
					}
				}
			}
		}

		.coupon-box {
			.coupon-item {
				background-color: #fff;
				border-radius: 10px;
				padding: 20px;
				position: relative;
				margin: 15px 20px 0 20px;

				.header-box {
					display: flex;
					align-items: center;

					.header-left-box {
						width: 70px;
						flex: 0 0 70px;
						display: flex;
						flex-direction: column;
						align-items: center;
						justify-content: center;

						.coupon-type {
							font-size: 16px;
							color: rgba(255, 165, 61, 1);
							font-weight: 500;
						}

						.get-coupon-num {
							font-size: 10px;
							color: rgba(0, 0, 0, 0.50);
							margin-top: 3px;
						}

						&.expired {
							.coupon-type {
								color: rgba(0, 0, 0, 0.50);
							}
						}
					}

					.header-right-box {
						flex: 1;
						margin-left: 10px;
						margin-top: 5px;

						.coupon-name {
							font-size: 18px;
							color: rgba(0, 0, 0, 0.90);
							font-weight: 500;
						}

						.coupon-date {
							font-size: 10px;
							color: rgba(0, 0, 0, 0.50);
							margin-top: 5px;
						}
					}
				}

				.line {
					width: 100%;
					height: 1px;
					background-color: rgba(0, 0, 0, 0.10);
					margin-top: 20px;
				}

				.bottom-box {
					margin-top: 14px;
					font-size: 10px;
					color: rgba(0, 0, 0, 0.50);
				}

				.residue-box {
					position: absolute;
					right: 0;
					top: 0;
					width: 65px;
					height: 33px;
					background-image: url("../../static/coupon/coupon-list-right-icon.png");
					background-repeat: no-repeat;
					background-size: 100%;
					border-top-right-radius: 10px;
					display: flex;
					align-items: center;
					justify-content: center;
					padding-left: 5px;
					padding-bottom: 6px;
					box-sizing: border-box;

					&.expired {
						background-image: url("../../static/coupon/coupon-expired-right-icon.png");
					}

					.residue-num {
						font-size: 15px;
						font-weight: 500;
						color: rgba(255, 255, 255, 1);

						&.expired-num {
							font-size: 13px;
						}
					}
				}
			}

			.no-data-tips {
				margin-top: 40px;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;

				text {
					margin-top: 10px;
					font-size: 14px;
					color: #999;
				}
			}
		}
	}
</style>